<template>
  <div class="pages">
    <!-- <div class="pageLevel" v-if="!sub"> -->
    <!-- 头部 -->
    <div class="head">
      <div class="head-search">
        <span>公告状态</span>
        <el-select v-model="search.status" placeholder="请选择" size="small" class="select-search">
          <el-option
            v-for="item in noticeStatus"
            :key="item.name"
            :label="item.label"
            :value="item.name"
          ></el-option>
        </el-select>
        <span>公告类型</span>
        <el-select v-model="search.type" placeholder="请选择" size="small" class="select-search">
          <el-option
            v-for="item in noticeType"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-input class="input" v-model="search.name" size="small" placeholder="请输入名称" clearable></el-input>
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="searchSelect"
          class="Button"
          size="small"
        >查询</el-button>
      </div>
      <div class="head-add">
        <el-button
          type="primary"
          @click=" routerPush('3', '')"
          size="mini"
          v-if="setRole('新增')"
        >新增公告</el-button>

      </div>
    </div>
    <div class="list-body">
      <el-table
        :data="tableDataList"
        stripe
        style="width: 100%"
        :operateShow="operateShow"
        :pageSize="pageSize"
        :pageNum="pageNum"
      >
        <el-table-column type="index" label="序号" width="50">
          <template slot-scope="scope">
            <div>{{scope.$index+ pageSize*pageNum +1}}</div>
          </template>
        </el-table-column>
        <el-table-column prop="position" label="公告类型" width="80" :formatter="forNoticeType"></el-table-column>

        <el-table-column prop="imgUrl" label="封面"  width="180">
          <template slot-scope="scope">
            <div v-if="scope.row.position == '3'">
              <img v-if="scope.row.otherAttributes.titlePage == 0" :src="coverlist.coverOne" class="head_pic" />
              <img v-if="scope.row.otherAttributes.titlePage == 1" :src="coverlist.coverTwo" class="head_pic" />
              <img v-if="scope.row.otherAttributes.titlePage == 2" :src="coverlist.coverThree" class="head_pic" />
            </div>
            <div class="goods-img" v-else >
              <img :src="scope.row.otherAttributes.goodsImage" class="head_pic" />
            </div>

          </template>
        </el-table-column>
        <el-table-column prop="title" label="名称" width="130"></el-table-column>
        <el-table-column prop="validityStartTime" label="上架时间" :formatter="trunTime"></el-table-column>
        <el-table-column prop="validityEndTime" label="下架时间" :formatter="trunTime"></el-table-column>
        <el-table-column prop="updateTime" label="更新时间" :formatter="trunTime"></el-table-column>
        <el-table-column prop="status" label="状态" :formatter="forSatus" width="80"></el-table-column>
        <el-table-column prop="priorityRating" label="权重值" width="80"></el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
             <el-button
              type="text"
              size="small"
              @click="editData(scope.row)"
              v-if="setRole('修改权重')"
            >修改权重</el-button>

            <el-button
              type="text"
              size="small"
              @click="routerPush(scope.row.status,scope.row.id)"
              v-if="setRole('编辑')"
            >编辑</el-button>

            <el-button
              type="text"
              size="small"
              @click="deletAdvertisement(scope.row,0)"
              v-if="scope.row.status != 1 && setRole('删除')"
            >删除</el-button>
            <el-button
              type="text"
              size="small"
              @click="deletAdvertisement(scope.row,1)"
              v-if="scope.row.status == 1 && setRole('下架')"
            >下架</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pageblock">
        <el-button size="small" type="primary" class="topage" @click="toPage">确定</el-button>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="['5','10','15',20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalItems"
        ></el-pagination>
      </div>
    </div>
    <!-- 修改权重弹窗 -->
      <el-dialog title="修改权重" :visible.sync="dialogVisible" width="375px" class="preview-dialog">
      <div class="preview-container">
       设置权重值 : <el-input size="small" v-model="changeData.changeWeight" @keyup.native="onLimitNumber(changeData.changeWeight)"></el-input>
       <div class="tip"><span>*</span>权重值越大，排序越靠前，权重值为正整数，可输入范围：0—10000</div>
      </div>
      <div style="text-align:right;margin-top:20px;">
        <el-button size="small" @click="dialogVisible = false;changeFlag = true">取 消</el-button>
        <el-button size="small" type="primary" @click="dialogSure">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import util from "@/util";
import s from "@/util/state";
import coverOne from "assets/images/weChatCoverOne.png";
import coverTwo from "assets/images/weChatCoverTwo.png";
import coverThree from "assets/images/weChatCoverThree.png";
import {
    noticeUpdParam,
    noticeList,
} from "@/api";
import { mapState } from "vuex";
export default {
  computed: mapState(["sub", "power", "merchantId"]),
  data() {
    return {
      dialogVisible: false,
      changeData: {
        changeWeight: 0,
        changeId:'',
        changeStatus:0
      },

      search: {
          status: '',
          type: '',
          name: ''
      },
      bannerType: "",
      classType: "info",
      listPosition: 0,
      listStatus: 1,
      operateShow: true,
      keyword: "",
      tableDataList: [],
      noticeStatus: [
        {
          name: '',
          label: "全部"
        },
          {
          name: 0,
          label:"待上架"
        },
        {
          name: 1,
          label: "进行中"
        },
        {
          name: 2,
          label: "已下架"
        }
      ],
      noticeType: [
          {
              label: '全部',
              value: ''
          },
          {
              label: '门店活动',
              value: 3
          },
          {
              label: '主推商品',
              value: 4
          }
      ],
      total: 0,
      pageSize: 20,
      totalItems: 0,
      currentPage: 1,
      pageNum: 0,
      coverlist:{
        coverOne: coverOne,
        coverTwo: coverTwo,
        coverThree: coverThree,
      }
    };
  },
  created() {
    this.getListData();
  },
  mounted() {},
  methods: {
    forSatus: s.noticeStatus,
    forNoticeType: s.noticeType,
   trunTime (row, column, cellValue) {
            return util.forDateAndTime(cellValue)
     },
    setRole(key) {
      return util.selectTitle(this.power, key);
    },

    getBannerList() {
      this.bannerType = "";
      this.classType = "info";
      this.listPosition = 0;
      this.getListData();
    },

    getClassList() {
      this.bannerType = "info";
      this.classType = "";
      this.listPosition = 1;
      this.getListData();
    },

    listdata(param) {
      this.getListData();
    },

    handleClick(type, event) {
      switch (type.name) {
        case "unStart":
          this.listStatus = 0;
          this.unStartListClick();
          break;
        case "running":
          this.listStatus = 1;
          this.runningListClick();
          break;
        case "closing":
          this.listStatus = 2;
          this.closingListClick();
          break;
      }
    },
    getListData() {
      let data = {
        companyId: this.merchantId,
        status: this.search.status,
        position: this.search.type,
        title: this.search.name,
        pageNo: this.currentPage,
        pageSize: this.pageSize,
        appId: 3 //(0:会员app,1:婴联通)',
      };
      noticeList(data).then(res => {
        this.tableDataList = res.data.list;
        this.totalItems = res.data.pageTotal;
      });
    },

    unStartListClick() {
      this.operateShow = false;
      this.getListData();
    },

    runningListClick() {
      this.operateShow = true;
      this.getListData( );
    },

    closingListClick() {
      this.operateShow = false;
      this.getListData();
    },

    searchSelect() {
      console.log(this.search)
      this.getListData();
    },

    toPage() {
      this.getListData();
      this.pageNum = this.currentPage - 1;
    },
    routerPush(status, id) {
      util.routerTo(
        "/operate-management/notice-management/notice-add",
        {
          status: status,
          id: id
        }
      );
    },
    editData(row) {
      this.changeData.changeWeight = row.priorityRating;
      this.changeData.changeId = row.id;
      this.dialogVisible = true
    },
    dialogSure(){
      let data = {
        id: this.changeData.changeId,
        priorityRating: this.changeData.changeWeight
      }
      noticeUpdParam(data).then(res => {
        if(res.code == 200) {
          this.$message.success('修改成功');
          this.getListData();
           this.dialogVisible = false;
        }
      })
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.pageNum = this.currentPage - 1;
      this.getListData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.pageNum = this.currentPage - 1;
      this.getListData();
    },
    deletAdvertisement(row,type) {
      let data = {
        id: row.id
      }, message = ''
      if(type == 0) {
        data.dr = 1
        message = '确定删除该条公告吗?'
      }else {
        data.status = 2
        message = '确定下架该条公告吗?'
      }
      this.$confirm(message, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消"
      })
        .then(() => {
          noticeUpdParam(data).then(res => {
              if (res.code == 200) {
                this.$message.success('操作成功');
                this.getListData();
              }
            })
        })
        .catch(() => {});

    },
    onLimitNumber(value) {
      this.changeData.changeWeight = util.replaceNumber(this.changeData.changeWeight);
      if(this.changeData.changeWeight>10000) {
        this.changeData.changeWeight = 0
        this.$message.error('输入范围在0-10000')
      }
    }
  }
};
</script>
<style lang="less" scoped>
.pages {
  padding: 20px 40px;
  .head {
    display: flex;
    justify-content: space-between;
    margin-bottom: 30px;

    .head-search {
      display: flex;
      justify-content: space-between;
      align-items: baseline
    }
  }
}
.el-tag {
  cursor: pointer;
}
.Button {
  margin-left: 10px;
}
.input {
  width: 30%;
}
.select-search {
  width: 20%
}
.el-dialog .el-input {
  width: 70%
}
img {
  width: 100%;
}
.tip{
  font-size: 12px;
  color: #868181;
  padding-top:10px;
  span {
    color:#fb4044;
  }
}
.goods-img {
  height: 66px;
}
.goods-img image {

}
</style>
